﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using DevComponents.DotNetBar;
using Public;
using Check_Barcode;
using IniFile;

namespace Check_Barcode
{
    public partial class Frm_login : DevComponents.DotNetBar.Office2007Form
    {
        //string ConStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\\DB\Location.mdb";//Access数据库相对路径

        DBAccessOperater db1;
        string dbstr = "";
        Class1 c1 = new Class1();
        public Frm_login()
        {

            InitializeComponent();
            ReadDataini();

        }

        #region 读取配置
        private void ReadDataini()
        {
            string sPath = Application.StartupPath + "\\Config.ini";//读取软件当前目录
            ClsIniFile ini = new ClsIniFile(sPath);
            string um = ini.IniReadValue("User", "user_name");
            string pwd = ini.IniReadValue("User", "Password");
            string sw1 = ini.IniReadValue("User", "ck_rem_user");
            string sw2 = ini.IniReadValue("User", "ck_rem_pwd");
            if (sw1 == "Checked")
            {
                checkBoxX1.Checked = true;
                this.txt_user_name.Text = um;
            }
            else
            {
                checkBoxX1.Checked = false;
                this.txt_user_name.Text = "";
            }
            if (sw2 == "Checked")
            {
                checkBoxX2.Checked = true;
                this.txt_Password.Text = pwd;
            }
            else
            {
                checkBoxX2.Checked = false;
                this.txt_Password.Text = "";
            }
        }
        #endregion

        private void Write_ini()
        {
            // 写入INI
            string sPath = Application.StartupPath + "\\Config.ini";//写入到软件当前目录
            ClsIniFile cl = new ClsIniFile(sPath);
            cl.IniWriteValue("User", "user_name", txt_user_name.Text);
            cl.IniWriteValue("User", "Password", txt_Password.Text);
            cl.IniWriteValue("User", "ck_rem_user", checkBoxX1.CheckState.ToString());
            cl.IniWriteValue("User", "ck_rem_pwd", checkBoxX2.CheckState.ToString());
        }


        private void Frm_login_Load(object sender, EventArgs e)
        {
            dbstr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\\DB\Location.mdb";//Access数据库相对路径;);
            db1 = new DBAccessOperater(dbstr);
            txt_user_name.Focus();

        }


        //SQLliteHelper help = new SQLliteHelper();
        private void btn_login_Click(object sender, EventArgs e)
        {
            try
            {
                string temp_Name = txt_user_name.Text;
                string temp_Password = txt_Password.Text;
                bool Flag_Login = false;
                string str_sql_cmdSearch = "SELECT * FROM [tb_user] where user='" + temp_Name + "' and password='" + temp_Password + "'";
              
                if (db1.CheckSQL(str_sql_cmdSearch))
                {
                    Flag_Login = true;
                  
                    c1.user=temp_Name;
                    c1.password = temp_Password;
                    Write_ini();

                }
              
                if (Flag_Login)
                {
                    //Frm_Print_mark1 main = new Frm_Print_mark1();
                    Frm_main main = new Frm_main();
                    //AForge_Barcode_Check main = new AForge_Barcode_Check();
                    //Frm_Voice_report main = new Frm_Voice_report();
                    main.Show();
                    this.Hide();
                }
                else
                {
                    Flag_Login = false;
                    MessageBoxEx.Show("用户名或密码错误");
                    return;
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Application.ExitThread();
        }

        private void txt_user_name_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_Password.Focus();

            }
        }

        private void txt_Password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_login_Click(sender, e);
            }

        }
    }
}
